
#######################
-->> ABOUT
#######################
(*

Phil Stokes -- 2018
applehelpwriter.com
sqwarq.com
*)
#######################
-->> DESCRIPTION
#######################
(*

For use with DetectX Swift's Folder observer function

*)
#######################
-->> USAGE
#######################
(*

There are no user options for this script.
Attempting to modify or move it will render the app unlaunchable. In that event, re-download
a new copy from Sqwarq.com or restore from your backup.

*)
#######################
-->> IMPORT STATEMENTS
#######################
use AppleScript version "2.4"
use framework "Foundation"
use framework "AppKit"
use scripting additions
#######################
-->> VARIABLES
#######################
property NSString : a reference to current application's NSString
property NSWorkspace : a reference to current application's NSWorkspace
property NSCharacterSet : a reference to current application's NSCharacterSet
set defaultWhitelist to {"com.sqwarq.DetectX-Swift.observer.plist", ".DS_Store", ".localized"}
set globalWhitelist to defaultWhitelist
set notInCurrent to {}
set notInPrevious to {}
#######################
-->> HANDLERS
#######################

on buildWhitelist()
(* These defaults preferences are written by DetectX Swift. Attempts to change them locally are ignored. *)
-- check reg status
set localRGU to ""
set ignKyw to ""
try
set localRGU to (do shell script "defaults read com.sqwarq.DetectX-Swift fobkey")
end try
try
set ignKyw to (do shell script "defaults read com.sqwarq.DetectX-Swift ignoreKeywords")
end try

if localRGU is "1" and ignKyw is "1" then
try
-- add user whitelist items to global whitelisting
set userWhiteList to ""
try
set userWhiteList to (do shell script "defaults read com.sqwarq.DetectX-Swift FobWhitelist")
end try
try
set userWhiteList to paragraphs of userWhiteList
end try
repeat with i from 1 to count of userWhiteList
set w to item i of userWhiteList
if w's length is greater than 2 then
set wString to (current application's NSString's stringWithString:w)
set w to (wString's stringByTrimmingCharactersInSet:(NSCharacterSet's whitespaceAndNewlineCharacterSet()))
set w to (w's stringByTrimmingCharactersInSet:(NSCharacterSet's punctuationCharacterSet()))
set w to (w's stringByTrimmingCharactersInSet:(NSCharacterSet's whitespaceAndNewlineCharacterSet()))
set w to w as text
set end of my globalWhitelist to w
end if
end repeat

on error
-- if the whitelist is malformed, remove it
try
do shell script "defaults delete com.sqwarq.DetectX-Swift FobWhitelist"
end try
set my globalWhitelist to my defaultWhitelist
end try
else
-- if user isn't registered, remove any previous whitelisting
try
do shell script "defaults delete com.sqwarq.DetectX-Swift FobWhitelist"
end try
set my globalWhitelist to my defaultWhitelist
end if
end buildWhitelist


on removeWhiteSpace:aString
set theString to current application's NSString's stringWithString:aString
set theWhiteSet to current application's NSCharacterSet's whitespaceAndNewlineCharacterSet()
set theString to theString's stringByTrimmingCharactersInSet:theWhiteSet
return theString as text
end removeWhiteSpace:


on listFolder:POSIXPath
set theFiles to {}
set fileManager to current application's NSFileManager's defaultManager()
set theNames to (fileManager's contentsOfDirectoryAtPath:POSIXPath |error|:(missing value)) as list
repeat with i from 1 to count of theNames
set thisFile to item i of theNames
if thisFile is not in my globalWhitelist then
set end of theFiles to thisFile
end if
end repeat
return theFiles
end listFolder:


on checkChanges()
set changeList to {}
set resultList to {}
set aResult to true
set usr to current application's NSUserName() as text
set ula to "/Users/" & usr & "/Library/LaunchAgents"
set la to "/Library/LaunchAgents"
set ld to "/Library/LaunchDaemons"
set watchPaths to {{"localLAs", la}, {"localLDs", ld}}
set doesExist to current application's NSFileManager's defaultManager's fileExistsAtPath:ula
if doesExist then
set end of watchPaths to {"userLAs", ula}
end if
repeat with path in watchPaths
set thePrevious to {}
set theCurrent to {}
set observed to {}
try
set observed to paragraphs of (do shell script "defaults read com.sqwarq.DetectX-Swift " & path's item 1)
end try
if (count of observed) is greater than 0 then
repeat with observed_item in observed
set obs to (my removeWhiteSpace:observed_item)
if obs is not in my globalWhitelist then
set end of thePrevious to obs
end if
end repeat
-- query the FileManager
try
set theCurrent to (my listFolder:(path's item 2))
end try
-- assess changes removed
repeat with a from 1 to (count thePrevious)
set aResult to true
set thisChange to item a of thePrevious
if (thisChange is not in theCurrent) then
repeat with w in my globalWhitelist
if w is in thisChange then
set aResult to false
end if
end repeat
set end of resultList to aResult
if aResult is true then
set end of changeList to thisChange
end if
end if
end repeat
-- assess changes added
repeat with b from 1 to count theCurrent
set aResult to true
set thisChange to item b of theCurrent
if (thisChange is not in thePrevious) then
repeat with w in my globalWhitelist
if w is in thisChange then
set aResult to false
end if
end repeat
set end of resultList to aResult
if aResult is true then
set end of changeList to thisChange
end if
end if
end repeat
end if
end repeat
if resultList contains true and ((count of changeList) > 0) then
return {true, changeList}
end if
return {false, missing value}
end checkChanges


#################
-->> COMMANDS
#################
set isEnabled to 0
try
set isEnabled to (do shell script "defaults read com.sqwarq.DetectX-Swift folderObserver")
end try
if isEnabled is "1" then
my buildWhitelist()
try
set notFirstRun to 0
try
set notFirstRun to (do shell script "defaults read com.sqwarq.DetectX-Swift notFirstRun")
end try
if notFirstRun is greater than 0 then
set shouldLaunch to my checkChanges()
if shouldLaunch's item 1 is true then
if (count of shouldLaunch's item 2) is less than 4 then
set theItem to return
repeat with anItem in shouldLaunch's item 2
set theItem to theItem & anItem & return
end repeat
else
set theItem to "4 or more changes detected." & return
end if

set aString to NSString's stringWithString:"com.sqwarq.DetectX-Swift"
-- ensure the main app is available on the system
set bundlePath to NSWorkspace's sharedWorkspace's absolutePathForAppBundleWithIdentifier:aString
if bundlePath is not missing value then
set theIcon to path to resource "AppIcon.icns" in bundle (application "DetectX Swift")
set aString to aString as text
try
set theApp to (current application's NSRunningApplication's runningApplicationsWithBundleIdentifier:aString)'s firstObject()
set theURL to theApp's its bundleURL()
try
theApp's forceTerminate()
repeat while theApp's isTerminated() is false
delay 0.1
end repeat
end try

set dialogAnswer to display dialog "Items: " & theItem & return & "Some background launch items have just been changed on your mac which can affect its security." & return & return & "Would you like to open DetectX Swift to review the changes?" buttons {"Cancel", "Open"} default button "Open" with title "DetectX Swift Folder Observer" with icon theIcon
if button returned of dialogAnswer is "Open" then
tell application id aString to activate
end if
end try
end if
end if
end if
on error number -128 -- userCanceledErr
--
end try
end if
#######################
#EOF



